/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package pfc.generacion;

import java.io.File;
import java.io.FileWriter;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.commons.lang.StringUtils;
import pfc.comun.Proyecto;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;
import pfc.comun.IDEpfc;
/**
 *
 * @author xandru
 */
public class GeneradorAntMakefile {

    Proyecto _proyecto;
    String _dir ;
    Element root;

    GeneradorAntMakefile(Proyecto pro, String _path) {
      this._proyecto = pro;
      this._dir = _path;
    }

    void generarAntMakeFile(String appName) {
        String builfFile = this._dir + File.separator + "build.xml";
        IDEpfc.log("\n=============== Generando fichero build para ANT ===================== ");
        IDEpfc.logTime("generado " + builfFile);
        Document doc = null;
        try {

            doc = DocumentHelper.createDocument();

            root = doc.addElement("project");
            root.addAttribute("name", _proyecto.nombre);

            Element ele = root.addElement("description");
            ele.addText("generado automaticamente para la aplicacion " + appName);

            addBuildLocationProperty("src", _proyecto.getSrcFolderName());
            addBuildLocationProperty("build", _proyecto.getBuildFolderName());
            addBuildLocationProperty("dist", _proyecto.getDistFolderName());

            generarInitTask();
            generarAntCleanTask();
            generarAntBuildTask();
            generarAntDistTask();
            generarAntDeployTask();



            OutputFormat format = OutputFormat.createPrettyPrint();
            XMLWriter writer = new XMLWriter(new FileWriter(builfFile), format);
            writer.write(doc);
            writer.flush();

        } catch (Exception ex) {
            Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, null, ex);
        }
    }

    private void addBuildLocationProperty(String key, String value) {
            Element ele = root.addElement("property");
             ele.addAttribute("name", key);
             ele.addAttribute("location",value);
    }

    private void generarAntCleanTask() {
        Element ele = root.addElement("target");
        ele.addAttribute("name", "clean");
        ele.addAttribute("description", "limpieza de compilaciones anteriores");
        Element delete = ele.addElement("delete");
        delete.addAttribute("dir","${build}");
        delete = ele.addElement("delete");
        delete.addAttribute("dir","${dist}");
    }

    private void generarAntBuildTask() {
        String jdkHome = IDEpfc.getPropertyValue("javahome");
        String javac = jdkHome + File.separator + "bin" + File.separator + "javac";
        String warFileName = this._proyecto.nombre + ".war";
        String compilelibs = IDEpfc.getPropertyValue("compilelibs");
        String distriblibs = IDEpfc.getPropertyValue("distriblibs");
        String tomcatHome = IDEpfc.getPropertyValue("tomcathome");

        String servletlib = tomcatHome + File.separator + "lib" + File.separator + "servlet-api.jar";

        Element ele = root.addElement("target");
        ele.addAttribute("name", "build");
        ele.addAttribute("depends", "init,clean");

        Element mdir = ele.addElement("mkdir");
        mdir.addAttribute("dir", "${build}");
        mdir = ele.addElement("mkdir");
        mdir.addAttribute("dir","${dist}");

        Element compile = ele.addElement("javac");
        compile.addAttribute("srcdir","${src}");
        compile.addAttribute("destdir","${build}");
        compile.addAttribute("fork", "yes");
        compile.addAttribute("executable", javac);

        Element classpath = compile.addElement("classpath");
        for (String s: StringUtils.split(compilelibs, ";"))
        {
            Element pathElement = classpath.addElement("pathelement");
            pathElement.addAttribute("location",s);
        }

        Element pathElement = classpath.addElement("pathelement");
        pathElement.addAttribute("location",servletlib);


        String webxml = this._dir + File.separator + "web" + File.separator + "WEB-INF" + File.separator + "web.xml";
        Element war = ele.addElement("war");
        war.addAttribute("destfile", "${build}" + File.separator+ warFileName);
        war.addAttribute("webxml", webxml);

        String web = this._dir + File.separator + "web";
        Element fileset = war.addElement("fileset");
        fileset.addAttribute("dir", web);

        
         for (String s: StringUtils.split(distriblibs, ";"))
        {
            Element libs = war.addElement("lib");
            libs.addAttribute("file",s);
        }


        Element classes = war.addElement("classes");
        classes.addAttribute("dir", "${build}" );

    }

    private void generarAntDistTask() {
        Element ele = root.addElement("target");
        ele.addAttribute("name", "dist");
        ele.addAttribute("depends", "build");
        Element mkdir = ele.addElement("mkdir");
        mkdir.addAttribute("dir","${dist}/lib/");
        Element jar = ele.addElement("jar");
        jar.addAttribute("jarfile","${dist}/lib/MyProject-${DSTAMP}.jar");
        jar.addAttribute("basedir","${build}");

    }

    private void generarInitTask() {
        Element ele = root.addElement("target");
        ele.addAttribute("name", "init");
        Element t = ele.addElement("tstamp");
    }

    private void generarAntDeployTask() {
        String tomcathome = IDEpfc.getPropertyValue("tomcathome");
        String deploydir = IDEpfc.getPropertyValue("deploydir");
        String warFile = _proyecto.getBuildFolderName() + File.separator + _proyecto.nombre + ".war";


         Element ele = root.addElement("target");
        ele.addAttribute("name", "deploy");
        ele.addAttribute("depends", "build");
        Element ele2 = ele.addElement("copy");
        ele2.addAttribute("file", warFile);
        ele2.addAttribute("tofile", deploydir + File.separator +  _proyecto.nombre + ".war");

    }

}
